recyclerview是Listview的改良版,提供更靈活跟優化在布局檔內的限制,是製作滑動表單更優先的選擇。
MainActivity
MainAdapter
Mainactivity 與listview的MainActivity大同小異,先宣告一下adapter跟recyclerview與建立arraylistrecyclerView.setLayoutManager(new LinearLayoutManager(this));
建立一個預設(垂直)滾動的LinearLayout布局在Activity上,如果想要改成水平滾動的可以改成new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
在每筆資料後增添的分隔線,如果想改成配合水平滾輪的分隔線可以用 recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
MainAdapter一開始同樣要先建立一個arraylist存儲recyclerview產生的畫面
MainAdapter對於Recyclerview比較綁定viewholder來建立每筆資料的換面上的原件
recyclerview在建立新筆資料時,會先產生一個view
之後會在view上抓需要顯示的資料
最後回傳總共產生了多少筆資料
最後還是展示一下我做的簡單例子
Mainactivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="422dp"
android:layout_height="725dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
RecyclerviewAdapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/textview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="TextView"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="TextView"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>
Mainactivity
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerviewAdapter listAdapter;
static String[] data ={"abc","123","48763","hello world","apple"};
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 20; i++) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("number",String.format("%02d", i + 1));
hashMap.put("data","data : "+String.valueOf(data[new Random().nextInt(5)]));
arrayList.add(hashMap);
}
recyclerView = findViewById(R.id.recycleView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
listAdapter = new RecyclerviewAdapter(arrayList);
recyclerView.setAdapter(listAdapter);
}
}
RecyclerviewAdapter
class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.ViewHolder> {
private ArrayList<HashMap<String, String>> arrayList;
public RecyclerviewAdapter(ArrayList<HashMap<String, String>> arrayList) {
this.arrayList = arrayList;
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView tx1, tx2;
public ViewHolder(View itemView) {
super(itemView);
tx1 = itemView.findViewById(R.id.textview);
tx2 = itemView.findViewById(R.id.textview2);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleviewitem, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.tx1.setText(arrayList.get(position).get("number"));
holder.tx2.setText(arrayList.get(position).get("data"));
}
@Override
public int getItemCount() {
return arrayList.size();
}
}